package com.hjf.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.common.web.servlet.BaseServlet;
import com.commons.po.CustomerRole;
import com.commons.po.DeptPo;
import com.commons.po.MenuPo;
import com.commons.po.PageBean;
import com.commons.po.RoleMenu;
import com.commons.po.UserPo;
import com.commons.utils.ConstantUtils;
import com.commons.utils.StringUtils;
import com.hjf.service.CustomerRoleService;
import com.hjf.service.MenuPoService;
import com.hjf.service.RoleMenuService;
import com.hjf.service.impl.CustomerRoleServiceImpl;
import com.hjf.service.impl.MenuPoServiceImpl;
import com.hjf.service.impl.RoleMenuServiceImpl;

public class MenuPoServlet extends BaseServlet {
	MenuPoService menuPoService=new MenuPoServiceImpl();
	CustomerRoleService customerRoleService=new CustomerRoleServiceImpl();
	RoleMenuService roleMenuService=new RoleMenuServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String opr=req.getParameter("opr");
		UserPo currUser=this.getCurrUser(req.getSession());
		String pageIndex=req.getParameter("pageIndex");  //当前页码
		if("load".equals(opr)){
			int index=StringUtils.str2Integer(pageIndex==null?"1":pageIndex,false);
			Integer menuId=StringUtils.str2Integer(req.getParameter("did"),false);
			Map<String, Object> map=null;
			if(menuId!=null){
				map=new HashMap<String, Object>();
				map.put("menuId", menuId);
				req.setAttribute("grandpa",menuPoService.findMenuPoById(menuId).getMenuParent());
			}
			PageBean<MenuPo> pageBean=menuPoService.findPageByCondition(index, ConstantUtils.PAGE_SIZE, map);
			req.setAttribute("pageBean", pageBean);
			req.getRequestDispatcher("/jsp/sys/menu/menu_uplist.jsp").forward(req, resp);
			
		}else if("add".equals(opr)||"update".equals(opr)){
			loadData(req,resp,opr,pageIndex);
		
		//  禁用/激活 状态更改	
		}else if("isActive".equals(opr)){  
			MenuPo menu=menuPoService.findMenuPoById(StringUtils.str2Integer(req.getParameter("id"),false));
			Integer ia=menu.getIsActive();
			String mag="";
			if(ia.equals(1)){
				menu.setIsActive(0);
				mag="禁用";
			}else{
				menu.setIsActive(1);
				mag="激活";
			}
			int row=menuPoService.updateMenuPo(menu);
			PrintWriter out=resp.getWriter();
			if(row>0){
				out.println("<script type='text/javascript'>alert('"+mag+"成功');location.href='menuPoServlet?opr=load&pageIndex="+pageIndex+"';</script>");
			}else{
				out.println("<script type='text/javascript'>alert('"+mag+"失败');history.back();</script>");
			}
			
		}else{
			loadMenu(req, resp, currUser);
		}
	}
	private void loadMenu(HttpServletRequest req, HttpServletResponse resp, UserPo currUser)
			throws ServletException, IOException {
//		System.out.println("进入loadMenu");
		List<Integer> list=new ArrayList<Integer>();
		Integer userId=currUser.getId();
		List<CustomerRole> customerRolelist=customerRoleService.findById(userId); //根据用户id找出角色id
		for(CustomerRole cr:customerRolelist){
//			System.out.println("进入遍历");
			List<RoleMenu> roleMenuList=roleMenuService.findById(cr.getRoleId()); //根据角色id找出菜单id
			//把菜单id进行去重
			for(RoleMenu rm:roleMenuList){
				Integer menuId=rm.getMenuId();
				boolean isFlag=true;
				for(int i=0;i<list.size();i++){
					if(menuId==list.get(i)){
						isFlag=false;
						break;
					}
				}
				if(isFlag){
					list.add(menuId); //某个用户所拥有的所有菜单权限
				}
			}
		}
		List<MenuPo> menuPoList=new ArrayList<MenuPo>();
		for(Integer id:list){
			MenuPo menuPo=menuPoService.findMenuPoById(id);
			if(menuPo!=null){
				menuPoList.add(menuPo); //根据菜单id找出菜单名
				System.out.println(menuPo.getMenuName());
			}
		}
		
		
//		List<MenuPo> menuPoList=new ArrayList<MenuPo>();
//		Integer menuId=StringUtils.str2Integer(req.getParameter("menuId"),false);
//		if(menuId!=null){
//			List<MenuPo> sonList=menuPoService.findByMenuPoParent(menuId);
//			if(sonList!=null&&sonList.size()>0){
//				for(MenuPo mp:sonList){
//					menuPoList.add(mp);
//				}
//			}
//			Integer parentId=null;
//			do{
//				parentId=deptService.findById(deptId).get(0).getDeptParent();
//				if(parentId!=null){
//					List<DeptPo> otherList=deptService.findByDeptParent(parentId);
//					if(otherList!=null&&otherList.size()>0){
//						for(DeptPo d:otherList){
//							idList.add(d.getDeptId());
//						}
//					}
//					deptId=parentId;
//				}
//			}while(parentId!=null);
//		}
//		for(int i=0;i<list.size();i++){
//			DeptPo dept=list.get(i);
//			if(dept.getDeptParent()!=null){
//				boolean isFlag=true;
//				if(idList.size()>0){
//					for(Integer id:idList){
//						if(dept.getDeptId().equals(id)){
//							isFlag=false;
//						}
//					}
//					if(isFlag){
//						list.remove(i);
//						i=i-1;
//					}
//					isFlag=true;
//				}else{
//					list.remove(i);
//					i=i-1;
//				}
//			}
//		}
		
		
		
		
//		List<MenuPo> menuPoList=new ArrayList<MenuPo>();
//		for(Integer id:list){
//			Integer menuId=StringUtils.str2Integer(req.getParameter("menuId"),false);
//			MenuPo menuPo=menuPoService.findMenuPo(id,menuId);
//			if(menuPo!=null){
//				menuPoList.add(menuPo); //根据菜单id找出菜单名
//				System.out.println(menuPo.getMenuName());
//			}
//		}
		req.setAttribute("menuPoList", menuPoList);
		req.getRequestDispatcher("/jsp/sys/menu/menu_list.jsp").forward(req, resp);
	}
	
	/*新建或修改部门时，需加载的数据*/
	private void loadData(HttpServletRequest req, HttpServletResponse resp,String opr,String pageIndex) throws ServletException, IOException {
		List<MenuPo> list=menuPoService.findMenuPoAll(null);
		if("add".equals(opr)){
			req.setAttribute("login", list);
			req.getRequestDispatcher("/jsp/sys/menu/menu_save.jsp").forward(req, resp);
		}else{
			Integer id=Integer.parseInt(req.getParameter("id"));
			MenuPo menuPo=menuPoService.findMenuPoById(id);
			req.setAttribute("menuPo", menuPo);
			for(int i=0;i<list.size();i++){
				MenuPo menu=list.get(i);
				if(menu.getMenuId().equals(id)){
					list.remove(i);
					i=i-1;
				}
			}
			req.setAttribute("login", list);
			req.getRequestDispatcher("/jsp/sys/menu/menu_save.jsp?pageIndex="+pageIndex).forward(req, resp);
		}
	}
}
